package com.wxtt.android.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

import com.fast.dev.frame.Adapter.listview.BaseListAdapter;
import com.fast.dev.frame.http.callback.JsonHttpCallBack;
import com.fast.dev.frame.utils.AndroidInfoUtils;
import com.wxtt.android.R;
import com.wxtt.android.activity.VideoPlayActivity;
import com.wxtt.android.adapter.VideoAdapter;
import com.wxtt.android.bean.VideoBean;
import com.wxtt.android.config.ApiConfig;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 说明：视频
 * <p/>
 * 作者：fanly
 * <p/>
 * 时间：2016/2/21 18:06
 * <p/>
 * 版本：verson 1.0
 */
public class VideoFragment extends BaseListFragment<VideoBean> {

    private VideoAdapter mAdapter;
    private List<VideoBean> datas = new ArrayList<>();

    //当前请求的页码
    private int currentPage = 0;
    //每页请求的条数
    private int pageNumber = 20;

    @Override
    public BaseListAdapter<VideoBean> setAdapter() {
        lv.setOnItemClickListener(this);
        mAdapter = new VideoAdapter(lv,datas, R.layout.item_video);
        return mAdapter;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(mActivity, VideoPlayActivity.class);
        Bundle bundle = new Bundle();
        bundle.putParcelable("video",datas.get(position));
        intent.putExtra("video",bundle);
        mActivity.showActivity(mActivity,intent);
    }

    private JsonHttpCallBack callBack = new JsonHttpCallBack() {
        @Override
        public void onSuccess(JSONObject result) {
            List<VideoBean> data = new VideoBean().toList(result.optString("videos"));
            datas.addAll(0, data);
            mAdapter.refresh(datas);
        }

        @Override
        public void onFailure(int errorCode, String msg) {
            currentPage--;
            if (datas.isEmpty()){
                empty_bridge.setFailState(getString(R.string.loading_fail),erroListener);
            }else {
                mActivity.shortMessage(lv, msg);
            }
        }

        @Override
        public void onFinish() {
            super.onFinish();
            swipe_refresh.setRefreshLoadedState();
            if (!datas.isEmpty()){
                empty_bridge.setSuccessState();
            }
        }
    };

    @Override
    public void onRefresh() {
        if (currentPage < 0) currentPage = 0;
        currentPage++;
        ApiConfig.queryVideo(currentPage, pageNumber, "搞笑",callBack);
    }

    @Override
    public void onFirstUserVisible() {
        super.onFirstUserVisible();
        empty_bridge.setMyView(swipe_refresh);
        initFirst();
    }

    private void initFirst(){
        empty_bridge.setLoadingState();
        if (AndroidInfoUtils.isNetConnected(mActivity)){
            onRefresh();
        }else {
            empty_bridge.setNetWorkError(getString(R.string.not_network),erroListener);
        }
    }

    private View.OnClickListener erroListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            initFirst();
        }
    };
}
